<template>
  <div id="login">
    <div class="logo"><img :src="logoUrl" alt="logo"></div>
    <div class="banner">
      <div class="bg" :style="'height:' + contentHeight + 'px'">
        <div class="login_container">
          <div id="login-form">
            <section>
              <div class="login-label">用户登陆</div>
              <el-form :model="formData" :rules="formRules" ref="formData" class="demo-ruleForm">
                <el-form-item prop="username">
                  <div class="icon user"></div>
                  <el-input class="login-input" v-model="formData.username" autofocus auto-complete="off" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="operPasswd">
                  <div class="icon passwd"></div>
                  <el-input class="login-input" type="password" v-model="formData.operPasswd" auto-complete="off" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item prop="captcha">
                  <el-col :span="12">
                    <div class="icon captcha"></div>
                    <el-input class="login-input" disabledBlur @change="inputCaptcha" v-model="formData.captcha" @keyup.enter.native="login" auto-complete="off" placeholder="请输入验证码"></el-input>
                  </el-col>
                  <el-col :span="12">
                    <div class="img-captcha"><!-- 暂时关闭验证码功能 -->
                      <!-- <valid-code :captcha="formData.captcha" :validIcon="validIcon" @refreash="refreash"  @receive="receive" ref="validCode"></valid-code> -->
                    </div>
                  </el-col>
                </el-form-item>
                <el-form-item>
                  <el-button class="btn-submit" type="primary" size="large" @click="login" :loading="loading">登&emsp;&emsp;录</el-button>
                </el-form-item>
              </el-form>
            </section>
          </div>
        </div>
      </div>
    </div>
    <div class="copyright" v-html="copyRight"></div>
  </div>
</template>
<script>


// import { doLogin } from "@/api/user";
//import {getStore} from '@/utils/mUtils.js'
export default {
  props: {
    logoUrl: {
      type: String,
      default: 'static/img/logo.png'
    },
    // 登陆成功跳转地址
    htmlUrl: {
      type: String,
      default: './index.html'
    },
    copyRight: {
      type: String,
      default: '版权所有 &copy 恒生保泰(广东)科技有限公司'
    }
  },
  data() {
    return {
      labelPosition: 'top',
      loginForm: {
        userName: '',
        password: '',
      },
      errorMsg: '',
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
      },
      contentHeight: 0,
      formData: {
        username: '',
        operPasswd: '',
        captcha: ''
      },
      isSuccess: false,
      message: '',
      loading: false,
      sessionId: '',
      ticket: '',
      timer: null,
      validIcon: '',
      formRules: {}
    };
  },
  created() {
    this.getMainHeight();
  },
  mounted() {
    window.addEventListener('resize', this.getMainHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getMainHeight);
  },
  methods: {
    // 计算main主体高度
    getMainHeight() {
      let curHeight = window.innerHeight - 127;
      this.contentHeight = curHeight < 400 ? 400 : curHeight;
    },
    refreash() {
      this.validIcon = '';
    },
    // 输入框校验验证码
    inputCaptcha() {
      this.isSuccess = false;
      // 防抖
      if (this.timer) {
        clearTimeout(this.timer);
      }
      this.validIcon = ''
      this.timer = setTimeout(() => {
        this.$refs.validCode.validateCaptcha();
        this.validIcon = 'el-icon-loading';
        setTimeout(() => {
          if (this.isSuccess) {
            this.validIcon = 'el-icon-check';
          } else {
            this.validIcon = 'el-icon-close';
          }
        }, 600)
      }, 800)
    },
    // 获取验证码的sessionId 与ticket
    receive(sessionId, ticket) {
      this.isSuccess = true;
      this.sessionId = sessionId;
      this.ticket = ticket;
      // 验证通过关闭加载框
      setTimeout(() => {
        this.validIcon = 'el-icon-check';
      }, 500)
    },
    // submitForm() {
    //   this.$refs.formData.validate((valid) => {
    //     let _this = this;
    //     if (valid) {
    //       _this.loading = true;
    //       _this.message = '';
    //       post(loginServletUrl,
    //         {
    //           'data': {
    //             username: _this.formData.username,
    //             password: md5(_this.formData.operPasswd),
    //             captcha: {
    //               sessionId: _this.sessionId,
    //               ticket: _this.ticket
    //             }
    //           }
    //         },
    //         resp => {
    //           _this.loading = false;
    //           if (!resp.data.result) {
    //             _this.formData.captcha = '';
    //             Message.error(resp.data.msg);
    //           } else {
    //             cacheUtil.setCookies('Authorization', resp.data.data);
    //             util.getCurrentUser(oper => {
    //               oper.current_dept_code = oper.c_login_dept_code;
    //               oper.current_dept_name = oper.c_login_dept_name;
    //               delete oper.menus;
    //               delete oper.c_auth_grps;
    //               cacheUtil.setCookies('oper', JSON.stringify(oper));
    //               window.location.href = this.htmlUrl;
    //             })
    //           }
    //         },
    //         err => {
    //           _this.loading = false;
    //         }
    //       )
    //     } else {
    //       return false;
    //     }
    //   })
    // },
    resetForm() {
      this.$refs.formData.resetFields();
    },

    login() {
      if (this.formData.username === '' || this.formData.operPasswd === '') {
        return;
      } else {
        this.$http({
          url: this.$http.adornUrl('/sys/login'),
          method: 'post',
          data: this.$http.adornData({
            username: this.formData.username,
            password: this.formData.operPasswd,
            uuid: "",
            captcha: "",
          }),
        })
          .then(({ data }) => {
            this.$store.dispatch('loading/CHANGE_LOADING', false);
            if (data && data.code === 0) {
              localStorage.clear();
              sessionStorage.clear();
              this.$store.commit('updatechecktask', {});
              this.$store.commit('checkChanel', {});
              // console.log(this.$store.state, 'this.$store');
              this.$cookie.set('token', data.token);
              this.$router.replace({ name: 'home' });
              this.$store.commit('user/updateName', this.formData.username);
            } else {
              this.$message.error(data.msg);
            }
          })
          .catch((err) => {
            this.$store.dispatch('loading/CHANGE_LOADING', false);
          });
        // 原登录
        //     let obj={};
        //     obj.password=this.loginForm.password;
        //     obj.userCode=this.loginForm.userName;
        //   this.$store.dispatch('loading/CHANGE_LOADING', true);
        //   doLogin(obj).then(res => {
        //     this.$store.dispatch('loading/CHANGE_LOADING', false);
        //     if (res.data.resultCode == '0' && res.data.data) {
        //       this.errorMsg='';
        // 	        // let tmp = getStore('newUndr_randomStr');
        //       // localStorage.clear();
        // 	        // res.data['newUndr_randomStr']=tmp;
        //       // this.$store.dispatch('LogOut', {});
        //       // sessionStorage.setItem("loginForm",JSON.stringify(this.loginForm))
        //       // this.$store.dispatch('saveUserLogin', res.data);
        //       debugger
        //       this.$cookie.set('token', res.data.data.token)
        //       this.$router.replace({ name: 'home' })
        //       this.$store.commit('user/updateName', res.data.data.userKeyInfo.account)
        //     }else{
        //         this.errorMsg=res.data.data.resultMsg;
        //     }
        //   }).catch(err => {
        //     // console.log(err);
        //         this.errorMsg=err.resultMsg;
        //     this.$store.dispatch('loading/CHANGE_LOADING', false);
        //   });
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.logo {
  height: 50px;
  line-height: 50px;
  img {
    height: 40px;
    margin: 0 10px;
  }
}
.banner {
  padding: 0;
  // background-color: #106dc3;
  text-align: center;
  background: linear-gradient(to bottom, #a7cbff 50%, #0b4a85 50%);
}
.banner .bg {
  margin: 0 auto;
  // height: 620px;
  background-repeat: no-repeat;
  background-size: 100% auto;
  background-position: 25%;
  background-image: url("~static/img/banner_bg.jpg");
}
.login_container {
  position: relative;
  top: 40px;
  padding: 0 128px;
}
#login-form {
  float: right;
  width: 380px;
  height: 380px;
  padding: 15px 30px;
  margin: 0 auto;
  border-radius: 8px;
  background-color: #fff;
  text-align: left;
}
.copyright {
  text-align: center;
  background-color: #0b4a85;
  padding: 38px 0 20px 0;
  // color: #106dc3;
  color: #ffffff;
}
.icon {
  width: 40px;
  height: 40px;
  position: absolute;
  z-index: 3;
  background-position: center;
  background-repeat: no-repeat;
}
.login-input /deep/input {
  font-size: 14px;
  color: #333;
  border-radius: 4px;
  height: 40px !important;
  line-height: 40px;
  text-indent: 40px;
  padding: 0;
}

.icon.user {
  background-image: url("~static/img/login/user.png");
}

.icon.passwd {
  background-image: url("~static/img/login/pass.png");
}

.icon.captcha {
  background-image: url("~static/img/login/yzm.png");
}

.btn-submit {
  width: 100%;
  background-color: #0275d8;
  border-color: #0275d8;
  border-radius: 4px;
  font-size: 20px;
  color: #ffffff;
  height: 50px;
}
.btn-submit:hover {
  background-color: #025aa5;
}

.login-label {
  font-size: 22px;
  margin: 24px 0;
}

.img-captcha {
  width: 100%;
  padding-left: 20px;
  display: table-cell;
  cursor: pointer;
}

.login_container /deep/ .el-form-item__content {
  margin-bottom: 20px;
}
.login_container /deep/ .el-form-item__error {
  position: absolute;
  padding: 5px;
}

.btn-submit:focus {
  outline: none;
  background-color: #0275d8;
}
</style>
